package servlet;

import it.polimi.swimv2.exception.ModifyException;
import it.polimi.swimv2.exception.UserDataException;
import it.polimi.swimv2.remote.AdminManagerRemote;
import it.polimi.swimv2.remote.UserManagerRemote;
import it.polimi.swimv2.util.RemoteBindingName;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ModifyAdminKeyServlet
 */
public class ModifyAdminKeyServlet extends HttpServlet {
	
	private static final String SESSION_ADMIN = "admin";
	//Actions performed by this Servlet:
	private static final String ACT_PARAM = "act";
	private static final String SHOW_MODIFY_ADMIN_KEY = "showModifyAdminKey";
	private static final String ACT_MODIFY_PASSWORD = "modifyPassword";
	//Page references:
	private static final String MODIFY_ADMIN_PASSWORD_JSP = "modifyAdminKey.jsp";
	private static final String ERROR_JSP = "error.jsp";
	private static final String INDEX_JSP = "index.jsp";
	private static final String LOGIN_FORM_JSP = "loginForm.jsp";
	//Parameters in HttpRequest:
	private static final String CURRENT_PWD_REQPARAM = "currentPwd";
	private static final String NEW_PWD1_REQPARAM = "newPwd1";
	private static final String NEW_PWD2_REQPARAM = "newPwd2";
	private static final String ERROR_REQPARAM = "error";
	private static final String SUCCESS_PWD_ERRORVALUE = "successPwd";
	private static final String NEW_PWD_ERRORVALUE = "newPwd";
	private static final Object NEW_PWD_INVALID_ERRORVALUE = "illegalNewPwd";
	private static final String MODIFY_PASSWORD_ERRORVALUE = ACT_MODIFY_PASSWORD;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyAdminKeyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		chooseAction(request,response);
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		chooseAction(request, response);
	}
	
	/**
	 * manages the action requested by http request
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void chooseAction(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute(SESSION_ADMIN);
		String actParam = request.getParameter(ACT_PARAM);
		if(username==null){
			response.sendRedirect(LOGIN_FORM_JSP);
			return;
		}
		if (actParam==null) {
			response.sendRedirect(INDEX_JSP);
			return;
		}
		if(actParam.equals(SHOW_MODIFY_ADMIN_KEY)){
			showModifyAdminKey(request,response);
		}else if(actParam.equals(ACT_MODIFY_PASSWORD)){
			processModifyPassword(request,response);
		}
		
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void showModifyAdminKey(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = request
				.getRequestDispatcher(MODIFY_ADMIN_PASSWORD_JSP);
		dispatcher.forward(request, response);
		
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void processModifyPassword(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		
			AdminManagerRemote rem;
			try {
				rem = retrieveAdminManagerRemote();
				HttpSession session = request.getSession();
				String username = ((String) session.getAttribute(SESSION_ADMIN));
				String currentPwd = request.getParameter(CURRENT_PWD_REQPARAM);
				String newPwd1 = request.getParameter(NEW_PWD1_REQPARAM);
				String newPwd2 = request.getParameter(NEW_PWD2_REQPARAM);
				if (newPwd1.equals(newPwd2)) {
					rem.modifyPassword(username, currentPwd, newPwd1);
					request.setAttribute(ERROR_REQPARAM, SUCCESS_PWD_ERRORVALUE);
					showModifyAdminKey(request, response);
				} else {
					request.setAttribute(ERROR_REQPARAM, NEW_PWD_ERRORVALUE);
					showModifyAdminKey(request, response);
				}
			} catch (ModifyException e) {
				request.setAttribute(ERROR_REQPARAM, MODIFY_PASSWORD_ERRORVALUE);
				showModifyAdminKey(request, response);
			} catch (NamingException e) {
				response.sendRedirect(ERROR_JSP);
			} catch (UserDataException e) {
				request.setAttribute(ERROR_REQPARAM, NEW_PWD_INVALID_ERRORVALUE);
				showModifyAdminKey(request, response);
			}
		
		
	}
	/**
	 * 
	 * @return
	 * @throws NamingException
	 */
	private AdminManagerRemote retrieveAdminManagerRemote() throws NamingException {
			Context jndiContext = new InitialContext();
			Object ref = jndiContext.lookup(RemoteBindingName.ADMIN_MANAGER);
			AdminManagerRemote rem = (AdminManagerRemote) ref;
			return rem;		
	}

}
